<?php

if ($email_inc){
	return;
}
$email_inc= "defined";
define("smtpport",25);

class pop3 {
	var $subject; // 邮件主题
	var $from_email; // 发件人地址
	var $from_name; // 发件人姓名
	var $to_email; // 收件人地址
	var $to_name; // 收件人姓名
	var $body; // 邮件内容
	var $filename; // 文件名
	var $socket; // 当前的 socket
	var $line;
	var $status;

	function pop3_open($server, $port){
		$this->socket = fsockopen($server, $port);
		if ($this->socket <= 0){
			return false;
		}
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);
		if ($this->status[ "lastresult"] <> "+"){
			return false;
		}
		return true;
	}

	function pop3_user($user){
		if ($this->socket < 0){
			return false;
		}
		fputs($this->socket, "user $this->userrn");
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);

		if ($this->status[ "lastresult"] <> "+"){
			return false;
		}
		return true;
	}

	function pop3_pass( $pass){
		fputs($this->socket, "pass $passrn");
		$this->line = fgets($this->socket, 1024);
		$this->status["lastresult"] = substr($this->line, 0, 1);
		$this->status["lastresulttxt"] = substr($this->line, 0, 1024);
		if ($this->status["lastresult"] <> "+"){
			return 0;
		}
		return 1;
	}

	function pop3_stat(){
		fputs($this->socket, "statrn");
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);
		if ($this->status[ "lastresult"] <> "+") return 0;
		if (!eregi( "+ok (.*) (.*)", $this->line, $regs)){
			return 0;
		}
		return $regs[1];
	}

	function pop3_list(){
		fputs($this->socket, "listrn");
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);

		if ($this->status[ "lastresult"] <> "+"){
			return 0;
		}
		$i = 0;
		while (substr($this->line = fgets($this->socket, 1024), 0, 1) <> "."){
			$articles[$i] = $this->line;
			$i++;
		}
		$articles[ "count"] = $i;
		return $articles;
	}

	function pop3_retr($nr)	{
		fputs($this->socket, "retr $nrrn");
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);

		if ($this->status[ "lastresult"] <> "+"){
			return 0;
		}
		while (substr($this->line = fgets($this->socket, 1024), 0, 1) <> "."){
			$data[$i] = $this->line;
			$i++;
		}
		$data[ "count"] = $i;
		return $data;
	}

	function pop3_dele( $nr){
		fputs($this->socket, "dele $nrrn");
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);

		if ($this->status[ "lastresult"] <> "+"){
			return 0;
		}
		return 1;
	}

	function pop3_quit(){
		fputs($this->socket, "quitrn");
		$this->line = fgets($this->socket, 1024);
		$this->status[ "lastresult"] = substr($this->line, 0, 1);
		$this->status[ "lastresulttxt"] = substr($this->line, 0, 1024);

		if ($this->status[ "lastresult"] <> "+"){
			return 0;
		}
		return 1;
	}
} 